package com.zl.tesseract.scanner.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zl.tesseract.R;
import com.zl.tesseract.scanner.beans.EnterBean;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class FcListAdapter extends RecyclerView.Adapter<FcListAdapter.ViewHolder> {

    private Context mContext;

    private List<EnterBean> dataList;

    public FcListAdapter(Context context, List<EnterBean> list) {
        this.mContext = context;
        this.dataList = list;
    }

    public void setList(List<EnterBean> list) {
        dataList.clear();
        append(list);
    }

    public void append(List<EnterBean> list) {
        int positionStart = dataList.size();
        int itemCount = list.size();
        dataList.addAll(list);
        if (positionStart > 0 && itemCount > 0) {
            notifyItemRangeInserted(positionStart, itemCount);
        } else {
            notifyDataSetChanged();
        }
    }

    public void append(EnterBean item) {
        int positionStart = dataList.size();
        dataList.add(item);
        if (positionStart > 0) {
            notifyItemRangeInserted(positionStart, 1);
        } else {
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_fc_list, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final ImageView iv_brand_logo = holder.iv_brand_logo;
        final EditText edt_phone = holder.edt_phone;
        final ImageView iv_edit = holder.iv_edit;
        final EditText edt_order = holder.edt_order;
        final TextView tv_index = holder.tv_index;
        final Button delete = holder.delete;

        final EnterBean bean = dataList.get(position);

        String formatPhone = bean.getPhone().substring(0,3) + " "
                + bean.getPhone().substring(3,7) + " "
                + bean.getPhone().substring(7,11);
        edt_phone.setText(formatPhone);
        edt_order.setText(bean.getOrder_id());
        iv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_phone.setEnabled(true);
                edt_order.setEnabled(true);
            }
        });

        // 侧滑删除功能
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("是否确认删除!")
                        .setConfirmText("删除")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                sDialog.dismissWithAnimation();
                                dataList.remove(position);
                                notifyDataSetChanged();
                            }
                        })
                        .show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_brand_logo;
        EditText edt_phone;
        ImageView iv_edit;
        EditText edt_order;
        TextView tv_index;
        Button delete;

        ViewHolder(View itemView) {
            super(itemView);
            iv_brand_logo = itemView.findViewById(R.id.iv_brand_logo);
            edt_phone = itemView.findViewById(R.id.edt_phone);
            iv_edit = itemView.findViewById(R.id.iv_edit);
            edt_order = itemView.findViewById(R.id.edt_order);
            tv_index = itemView.findViewById(R.id.tv_index);
            delete = itemView.findViewById(R.id.delete);
        }
    }

}
